<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2025-07-25 10:54:36
 * @LastEditors: 小白
 * @LastEditTime: 2025-09-09 14:25:48
 * @FilePath: \tmsadmin\src\views\home\components\modalProduct.vue
-->
<template>
  <el-dialog v-model="dialogVisible" title="编辑产品" width="600px" :append-to-body="true" :before-close="handleClose">
    <el-form ref="ruleFormRef" :model="ruleForm" label-width="100px" :rules="rules" class="demo-ruleForm" status-icon>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="ruleForm.productName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="批次" prop="batchNo">
          <el-input v-model="ruleForm.batchNo" placeholder="请输入批次" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker v-model="ruleForm.startDate" :value-format="'YYYY-MM-DD HH:mm:ss'" :format="'YYYY-MM-DD HH:mm:ss'" style="width: 100%;" type="datetime" placeholder="选择开始时间" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker v-model="ruleForm.endDate" :value-format="'YYYY-MM-DD HH:mm:ss'" :format="'YYYY-MM-DD HH:mm:ss'" style="width: 100%;" type="datetime" placeholder="选择结束时间" />
          </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)" v-loading="loading">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
const ruleFormRef = ref<FormInstance>()
import type { UploadInstance } from 'element-plus'
const uploadRef = ref<UploadInstance>()
const title = ref('')
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const loading = ref(false)
const ruleForm = ref({
  id:'',
  // 产品名称
  productName: '',
  // 批次
  batchNo: '',
  // 开始时间
  startDate: '',
  // 结束时间
  endDate: '',
})

const rules = ref({
  productName: [
    { required: true, message: '请输入产品名称', trigger: 'blur' },
  ],
  batchNo: [
    { required: true, message: '请输入批次', trigger: 'blur' },
  ],
  startDate: [
    { required: true, message: '请输入开始时间', trigger: 'blur' },
  ],
  endDate: [
    { required: true, message: '请输入结束时间', trigger: 'blur' },
  ],
})

const openModal = ( e: any) => {
  ruleForm.value = e
  dialogVisible.value = true
}
const handleClose = () => {
  dialogVisible.value = false
  emits('ok')
}
const handleCloses = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emits('ok')
  dialogVisible.value = false
}
const submit = async (formEl: FormInstance | undefined) => {
  loading.value = true
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      REQUEST.POST('EDITPRODUCT', {
       batchList:[
        {
        id: ruleForm.value.id,
        productName: ruleForm.value.productName,
        batchNo: ruleForm.value.batchNo,
        startDate: ruleForm.value.startDate,
        endDate: ruleForm.value.endDate,
        }
       ]
      }).then((res: any) => {
        if (res.status == 200) {
          loading.value = false
          handleClose()
          emits('ok')
        } else {
          loading.value = false
          handleClose()
          ElMessage.warning(res.message)
        }
      })
    }
  })
}
defineExpose({ openModal })
</script>

<style lang="" scoped></style>
